﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Web;
using System.Web.UI;
using System.IO;
using MSXML2;

namespace SBS.Common
{
    public class remoteCall
    {
        /*
         * function webcall(string method, string url, string param, string header)
         * method : post ,get
         * url: http://www.domain.com
         * param: a=1&b=2&c=3
         * header: k=1; k2=2; k3=3  ( this set send url cookie parameter)
         */
        public string webcall(string method, string url, string param, string header)
        {
            HttpWebRequest rq = (HttpWebRequest)WebRequest.Create(url);
            rq.Method = method.ToUpper();
            if (method == "POST")
            {
                byte[] b = System.Text.Encoding.Default.GetBytes(param);
                rq.ContentType = "application/x-www-form-urlencoded";
                rq.ContentLength = b.Length;
                Stream sw = rq.GetRequestStream();
                sw.Write(b, 0, b.Length);
                sw.Close();
            }

            if (header != "")
            {
                rq.Headers.Add("cookie", "string");
                if (HttpContext.Current != null)
                {
                    rq.Headers.Add("cookie", HttpContext.Current.Request.Headers.Get("cookie") + "; " + header); //set cookie
                }
                else
                {
                    rq.Headers.Add("cookie", header); //set cookie
                }
            }
            return this.callBack(rq);
        }


        public string webcall(string method, string url)
        {
            return webcall(method, url, "", "");
        }

        public string webcall(string method, string url, string param)
        {
            return webcall(method, url, param, " ");
        }

        public string callBack(HttpWebRequest rq)
        {
            HttpWebResponse rp = (HttpWebResponse)rq.GetResponse();
            Stream sw = rp.GetResponseStream();
            StreamReader sr = new StreamReader(sw, System.Text.Encoding.Default);
            string info = sr.ReadToEnd();
            sr.Close();
            return info;
        }
    }
    public class RemoveCall
    {
        public static void RequestNoResponse(string Url, string Method, Hashtable Header, byte[] param)
        {
            Method = Method.ToUpper();
            HttpWebRequest rq = (HttpWebRequest)WebRequest.Create(Url);
            rq.Method = Method;

            if (Method == "POST")
            {
                rq.ContentType = "application/x-www-form-urlencoded";
                if (param != null && param.Length > 0)
                {
                    rq.ContentLength = param.Length;
                    Stream rs = rq.GetRequestStream();
                    rs.Write(param, 0, param.Length);
                    rs.Close();
                }
            }
            if (Header != null)
            {
                foreach (string key in Header.Keys)
                {
                    if (Header[key] == null) continue;
                    if (key.ToLower() == "cookie")
                    {
                        rq.Headers.Add(HttpRequestHeader.Cookie, Header[key].ToString());
                    }
                    else
                    {
                        rq.Headers.Add(key, Header[key].ToString());
                    }
                }
            }
        }

        public static void RequestNoResponse(string Url, string Method, Hashtable Header, Hashtable Param)
        {
            StringBuilder sb = new StringBuilder();
            foreach (string key in Param.Keys)
            {
                string value = "";
                if (Param[key] != null)
                {
                    value = HttpUtility.UrlEncode(Param[key].ToString());
                }
                sb.Append(key + "=" + value + "&");
            }
            string str = sb.ToString();
            str = str.Remove(str.Length - 1);
            RequestNoResponse(Url, Method, Header, System.Text.Encoding.Default.GetBytes(str));
        }

        public static HttpWebResponse Request(string Url, string Method, Hashtable Header, byte[] param)
        {
            Method = Method.ToUpper();
            HttpWebRequest rq = (HttpWebRequest)WebRequest.Create(Url);
            rq.Method = Method;

            if (Method == "POST")
            {
                rq.ContentType = "application/x-www-form-urlencoded";                
                if (param != null && param.Length > 0)
                {
                    rq.ContentLength = param.Length;
                    Stream rs = rq.GetRequestStream();
                    rs.Write(param, 0, param.Length);
                    rs.Close();
                }
            }
            if (Header != null)
            {
                foreach (string key in Header.Keys)
                {
                    if (Header[key] == null) continue;
                    if (key.ToLower() == "cookie")
                    {
                        rq.Headers.Add(HttpRequestHeader.Cookie, Header[key].ToString());
                    }
                    else
                    {
                        rq.Headers.Add(key, Header[key].ToString());
                    }
                }
            }

            return (HttpWebResponse)rq.GetResponse();
        }
        public static HttpWebResponse Request(string Url, string Method, Hashtable Header, string NameValue)
        {
            return Request(Url, Method, Header, System.Text.Encoding.Default.GetBytes(NameValue));

        }
        public static HttpWebResponse Request(string Url, string Method, Hashtable Header, Hashtable Param)
        {
            StringBuilder sb = new StringBuilder();
            foreach (string key in Param.Keys)
            {
                string value = "";
                if (Param[key] != null)
                {
                    value = HttpUtility.UrlEncode(Param[key].ToString());
                }
                sb.Append(key + "=" + value + "&");
            }
            string str = sb.ToString();
            str = str.Remove(str.Length - 1);
            return Request(Url, Method, Header, System.Text.Encoding.Default.GetBytes(str));
        }
        public static HttpWebResponse Request(string Url, string Method, Hashtable Param)
        {
            return Request(Url, Method, null, Param);
        }
        public static HttpWebResponse Request(string Url, string Method)
        {
            return Request(Url, Method, null, new byte[0]);
        }

        //只提交，不需要返回值

        public static string RequestUrl(string Url, string Method, Hashtable Header, byte[] Param)
        {
            Stream sw = Request(Url, Method, Header, Param).GetResponseStream();
            StreamReader sr = new StreamReader(sw, System.Text.Encoding.Default);
            string txt = sr.ReadToEnd();
            sr.Close();
            sw.Close();
            return txt;
        }
        public static string RequestUrl(string Url, string Method, Hashtable Header, string NameValue)
        {
            return RequestUrl(Url, Method, Header, System.Text.Encoding.Default.GetBytes(NameValue));
        }
        public static string RequestUrl(string Url, string Method, Hashtable Header, Hashtable Param)
        {
            StringBuilder sb = new StringBuilder();

            foreach (string key in Param.Keys)
            {
                string value = "";
                if (Param[key] != null)
                {
                    value = HttpUtility.UrlEncode(Param[key].ToString());
                }
                sb.Append(key + "=" + value + "&");
            }
            string str = sb.ToString();
            str = str.Remove(str.Length - 1);

            return RequestUrl(Url, Method, Header, System.Text.Encoding.Default.GetBytes(str));
        }
        public static string RequestUrl(string Url, string Method, Hashtable Param)
        {
            return RequestUrl(Url, Method, null, Param);
        }
        public static string RequestUrl(string Url, string Method)
        {
            return RequestUrl(Url, Method, null, new byte[0]);
        }
    }

    public class RemoveCallCom
    {
        public static ServerXMLHTTP60 Request(string Url, string Method, Hashtable Headers, Hashtable Param)
        {
            Method = Method.ToUpper();
            ServerXMLHTTP60 Server = new ServerXMLHTTP60();
            Server.open(Method, Url, false, null, null);
            if (Headers != null)
            {
                foreach (string key in Headers.Keys)
                {
                    string val = "";
                    if (Headers[key] != null)
                    {
                        val = Headers[key].ToString();
                    }
                    Server.setRequestHeader(key, val);
                }
            }
            if (Method == "POST")
            {
                Server.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            }
            string[] arr = new string[Param.Count];
            int counter = 0;
            if (Param != null)
            {
                foreach (string key in Param.Keys)
                {
                    string val = "";
                    if (Param[key] != null)
                    {
                        val = HttpUtility.UrlEncode(Param[key].ToString());
                    }
                    arr[counter] = key + "=" + val;
                    counter++;
                }
            }
            Server.send(counter > 0 ? string.Join("&", arr) : null);
            return Server;
        }

    }


}
